package com.ruoyi.map.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.ums.domain.entity.Member;
import com.ruoyi.chat.domain.FhChatFriend;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.map.mapper.H5ArtUserFriendMapper;
import com.ruoyi.map.service.IH5ArtUserFriendService;
import okhttp3.*;
import org.apache.commons.math3.random.RandomDataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * service业务处理
 */
@Service
public class H5ArtUserFriendServiceImpl implements IH5ArtUserFriendService {

    @Value("${txim.appid}")
    private String appKey;

    @Value("${txim.appkey}")
    private String secret;

    @Value("${txim.username}")
    private String adminAccount;

    @Value("${txim.userSig}")
    private String adminUsgin;

    @Autowired
    private H5ArtUserFriendMapper h5ArtUserFriendMapper;


    // 公共的im请求方法
    @Override
    public String sendRequest(String apiUrl, Map<String, Object> body){
        String domainName = "console.tim.qq.com";
        String sdkappid = appKey;
        String identifier = adminAccount;
        String usersig = adminUsgin;
        String random = String.valueOf(new RandomDataGenerator().nextLong(0, 4294967295L));
        String contentType = "json";

        // 构建请求 URL
        String url = String.format("https://%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=%s", domainName, apiUrl, sdkappid, identifier, usersig, random, contentType);

        // 创建 OkHttp 客户端
        OkHttpClient client = new OkHttpClient();

        // 将请求体转换为 JSON 字符串
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        String jsonBody = com.alibaba.fastjson.JSON.toJSONString(body);
        RequestBody requestBody = RequestBody.create(JSON, jsonBody);

        // 构建请求
        Request request = new Request.Builder().url(url).post(requestBody).build();

        // 发送请求并处理响应
        try {
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            Headers responseHeaders = response.headers();
            for (int i = 0; i < responseHeaders.size(); i++) {
                System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
            }
            String responseBody = response.body().string();
            System.out.println(responseBody);
            return responseBody;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }




}
